<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('药品关系模板维护')" />
</head>
<body>
<div class="container-div">
    <div class="row">

        <div class="col-sm-12 search-collapse">
            <form id="post-form">
                <div class="select-list">
                    <ul>
                        <li>
<!--                            药品类型：<select name="classname"  id="deviceType">
                            <option value="中成药类">中成药类</option>
                            &lt;!&ndash;                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>&ndash;&gt;
                            &lt;!&ndash; <option th:each="deviceType : ${deviceTypeList}" th:text="${deviceType}" th:value="${deviceType}"></option>&ndash;&gt;
                            <option value="食品类">食品类</option>
                            <option value="西药类">西药类</option>-->
                            模板名称：<input type="text" id="templateName" name="templateName" th:placeholder="模板名称"/>
                        </li>

                        </select>
                        </li>
                        <li>
<!--                            <a class="btn btn-primary btn-rounded btn-sm" onclick="search()"><i class="fa fa-search"></i>&nbsp;搜索</a>-->
                            <a class="btn btn-primary btn-rounded btn-sm" onclick=searchByName()><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>

                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="link-template-table"></table>
        </div>
    </div>

</div>


<th:block th:include="include :: footer" />
<script th:inline="javascript">

    var datas = [[${@dict.getType('sys_normal_disable')}]];
    var prefix = ctx + "baodao/linktemplate";
    function reset() {
        $.ajax({
            //  url: ctx + '/baodao/linktemplate/addTemplate',
            url: prefix + '/list',
            dataType: 'json',
            type: 'POST',
            success: function(result) {
                console.log(result.rows)
                $('#link-template-table').bootstrapTable('load', result.rows);
                $('#link-template-table').bootstrapTable('refresh');

            }
        })

    }
    function searchByName(){
                $.ajax({
                    //  url: ctx + '/baodao/linktemplate/addTemplate',
                    url: prefix + '/list',
                    dataType: 'json',
                    type: 'POST',
                    data: {templatename: $("#templateName").val()},
                    success: function(result) {
                        console.log(result.rows)
                        $('#link-template-table').bootstrapTable('load', result.rows);
                        $('#link-template-table').bootstrapTable('refresh');

                    }
                })
        console.log('重新查询')
    }

    $(function() {

        $.ajax({
            //  url: ctx + '/baodao/linktemplate/addTemplate',
            url: prefix + '/list',
            dataType: 'json',
            type: 'POST',
            success: function(result) {
                console.log(result.rows)
                $('#link-template-table').bootstrapTable('load', result.rows);
                $('#link-template-table').bootstrapTable('refresh');

            }
        })
/*        var options = {
            url: prefix + "/list",
            // createUrl: prefix + "/add",
            // updateUrl: prefix + "/edit/{id}",
            // removeUrl: prefix + "/remove",
            // exportUrl: prefix + "/export",
            /!*  sortName: "companyId",*!/
            modalName: "药品关系模板维护",
            columns: [{
                checkbox: true
            },
                {
                    field: 'templateName',
                    title: '模板名称'
                },
                {
                    field: 'madeTime',
                    title: '创建时间'
                },
            ]
        };
        $.table.init(options);*/
    });
</script>
<script>
    $('#link-template-table').bootstrapTable({
        singleSelect:true,
        detailView: true,  //父子表
        striped: true, //是否显示行间隔色
        pagination: true,//显示分页
        sidePagination: "client",
        pageNumber:1,
        pageSize: 10,
        pageList: [10, 25],

        //url: prefix2 + '/list',
        columns: [
            {checkbox: true},
            {
                field: 'templatename',
                title: '模板名称'
            } ,
            {
                field: 'madeTime',
                title: '创建时间'
            },
            {
                title: '操作',
                align: 'center',
                formatter: function(value, row, index) {
                    var actions = [];
                    /* actions.push('<a class="btn btn-info  btn-xs ' + addFlag + '" href="javascript:void(0)" onclick="$.operate.add(\'' + row.goodscode + '\')"><i class="fa fa-plus"></i>新增</a> ');*/
                    /*   actions.push('<a class="btn btn-success btn-xs ' + "editFlag" + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.goodscode + '\')"><i class="fa fa-edit"></i>编辑</a> ');*/
                    /* actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.postId + '\')"><i class="fa fa-remove"></i>删除</a>');*/
                    // actions.push('<a class="btn btn-danger btn-xs ' + "removeFlag" + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.goodscode + '\')"><i class="fa fa-remove"></i>删除</a>');
                    actions.push('<a class="btn btn-danger btn-xs ' + "removeFlag" + '" href="javascript:void(0)" onclick="remove(\'' + row.templatename + '\')"><i class="fa fa-remove"></i>删除</a>');
                    return actions.join('');
                }
            }],
        onExpandRow: function (index, row, $detail) {
            //oInit.InitSubTable(index, row, $detail);
            console.log("展开子表")
            console.log("行数据：" + "index:" + index )
            console.log(row)
            initSubTable(index,row,$detail)
        }
    });




    function remove(templateName) {
        console.log("删除药品关系模板 ： " + templateName)
        layer.confirm("删除药品关系模板 ： " + templateName + "吗",{
            btn: ['确认', '取消']
        }, function () {
            // 按钮1的事件
            $.ajax({
                //  url: ctx + '/baodao/linktemplate/addTemplate',
                url: prefix + '/deleteTemplate',
                dataType: 'json',
                type: 'POST',
                data: {templatename:templateName},
                success: function(result) {
                    layer.closeAll();
                    console.log(result)
                    //$('#link-template-table').bootstrapTable('load', result.rows);
                    $.ajax({
                        //  url: ctx + '/baodao/linktemplate/addTemplate',
                        url: prefix + '/list',
                        dataType: 'json',
                        type: 'POST',
                        success: function(result) {
                            console.log(result.rows)
                            $('#link-template-table').bootstrapTable('load', result.rows);
                            $('#link-template-table').bootstrapTable('refresh');

                        }
                    })

                }
            })
        }, function(){
           layer.closeAll();
        });


    }



    //初始化子表格(无线循环)
    initSubTable = function (index, row, $detail) {
        console.log("模板名： "  +  row.templatename)
        function queryParams(){
            let tmp = {"templatename":row.templatename};
            return tmp;
        }
        var parentid = row.id;
        var cur_table = $detail.html('<table></table>').find('table');
        //  var cur_table = $detail.html('<table><title>测试</title></table>').find('table');
        $(cur_table).bootstrapTable({
            url: ctx + 'baodao/linktemplate/queryTemplate',
            method: 'post',
            contentType: 'application/x-www-form-urlencoded',
            queryParams:queryParams(),
            //data: {'goodsCode':'401-01-330'},
            ajaxOptions: { id: parentid },
            uniqueId: "id",
            striped: true, //是否显示行间隔色
            pagination: true,//显示分页
            sidePagination: "client",
            pageNumber:1,
            pageSize: 4,
            pageList: [4, 25],
            columns: [
                {
                    field: 'showFlag',
                    title: '关联药品',
                    formatter: function (value) {
                        if(value == '1'){
                            return '推荐药'
                        }else
                        if(value == '2'){
                            return '关联药'
                        }else {
                            return value;
                        }

                    }
                },
                {
                    field: 'goodscode',
                    title: '商品码'
                },
                {
                    field: 'goodsname',
                    title: '商品名称'
                },
                {
                    field: 'mfrs',
                    title: '生产厂商'
                },
                {
                    field: 'norm',
                    title: '规格'
                },
                /*                {
                                    field: 'plucode',
                                    title: '关联药品推荐'
                                },*/
                /*                {
                                    title: '操作',
                                    align: 'center',
                                    formatter: function(value, row, index) {
                                        var actions = [];
                                        /!* actions.push('<a class="btn btn-info  btn-xs ' + addFlag + '" href="javascript:void(0)" onclick="$.operate.add(\'' + row.goodscode + '\')"><i class="fa fa-plus"></i>新增</a> ');*!/
                                        /!*   actions.push('<a class="btn btn-success btn-xs ' + "editFlag" + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.goodscode + '\')"><i class="fa fa-edit"></i>编辑</a> ');*!/
                                        /!* actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.postId + '\')"><i class="fa fa-remove"></i>删除</a>');*!/
                                        /!* actions.push('<a class="btn btn-danger btn-xs ' + "removeFlag" + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.goodscode + '\')"><i class="fa fa-remove"></i>删除</a>');*!/
                                        return actions.join('');
                                    }
                                }*/
                /*    {
                        field: 'mfrs',
                        title: '生产企业'
                    },
                    {
                        field: 'norm',
                        title: '包装规则'
                    },
                    {
                        field: 'plucode',
                        title: '条码'
                    },*/
            ],
            //无线循环取子表，直到子表里面没有记录
            onExpandRow: function (index, row, $Subdetail) {
                initSubTable(index, row, $Subdetail);
            }
        });
    };




</script>
</body>
</html>